package com.txzt.gaea.business.project.service.impl;

import com.txzt.gaea.business.project.domain.model.ProjectFund;
import com.txzt.gaea.business.project.domain.model.ProjectInfo;
import com.txzt.gaea.business.project.domain.model.ProjectNode;
import com.txzt.gaea.business.project.service.IProjectFundService;
import com.txzt.gaea.business.project.service.IProjectInfoService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.*;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.flow.vo.FlowBizStepVo;
import com.txzt.gaea.provide.service.FileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ProjectFundServiceImpl extends BaseServiceImpl<ProjectFund> implements IProjectFundService {

    @Autowired
    private FlowService flowService;
    @Autowired
    private IProjectInfoService projectInfoService;
    @Autowired
    private FileInfoService fileInfoService;


    @Override
    public ProjectFund selectProjectFundById(Long id) {
        ProjectFund projectFund = sqlManager.single(ProjectFund.class,id);
        List<FlowBizStepVo> flowStep = flowService.getFlowBizStep(projectFund.getFlowId());
        projectFund.setFlowStep(flowStep);
        projectFund.setCheck(TranslateUtil.getCheck(flowStep));
        return projectFund;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveProjectFund(ProjectFund projectFund) {
        projectFund.setCheckStatus(CheckStatus.CHECK.getCode());
        projectFund.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = sqlManager.insertTemplate(projectFund,true);
        //开启流程
        Long flowBizId = flowService.startFlow(projectFund.getId(), "tb_project_fund","check_status",
                CheckStatus.REJECT.getCode(), CheckStatus.FINISH.getCode(), FlowFlagEnum.FUND_EXPENDITURE.getCode());
        ProjectFund target = new ProjectFund();
        target.setId(projectFund.getId());
        target.setFlowId(flowBizId);
        sqlManager.updateTemplateById(target);
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectFund.getProjectId());
        fileInfoService.fileChange(null,projectFund.getFilePath(),projectInfo.getProjectNo(),2);
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateProjectFund(ProjectFund projectFund) {
        ProjectFund ori = sqlManager.single(ProjectFund.class,projectFund.getId());
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectFund.getProjectId());
        fileInfoService.fileChange(ori.getFilePath(),projectFund.getFilePath(),projectInfo.getProjectNo(),2);
        return sqlManager.updateTemplateById(projectFund);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int resubmitProjectFund(ProjectFund projectFund) {
        //开启流程
        Long flowBizId = flowService.startFlow(projectFund.getId(), "tb_project_fund","check_status",
                CheckStatus.REJECT.getCode(), CheckStatus.FINISH.getCode(), FlowFlagEnum.FUND_EXPENDITURE.getCode());
        ProjectFund ori = sqlManager.single(ProjectFund.class,projectFund.getId());
        projectFund.setCheckStatus(CheckStatus.CHECK.getCode());
        projectFund.setFlowId(flowBizId);
        int count = sqlManager.updateTemplateById(projectFund);
        //处理文件
        ProjectInfo projectInfo = projectInfoService.selectById(projectFund.getProjectId());
        fileInfoService.fileChange(ori.getFilePath(),projectFund.getFilePath(),projectInfo.getProjectNo(),2);
        return count;
    }
}
